# -*- coding: utf-8 -*-
'''
@文件    :quickPay.py
@说明    :合利宝快捷支付相关接口
@时间    :2025/08/02 15:05:23
@作者    :浪晋
@版本    :1.0
'''


from . import base
from .. import utils


class QuickPayClient(base.BaseClient):

    def first_pay_preorder(self, user_id, realName, idCardNo, cardNo, phone, amount, notify_url, merchantNo=None):
        """首次支付预下单"""
        url = self.base_url + "/trx/quickPayApi/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'P2_customerNumber': merchantNo or self.merchant_number,
            "P3_userId": user_id,
            "P_orderId": utils.parameter.generate_order_no(),
            "P5_timestamp": utils.parameter.generate_timestamp(),
            "P6_payerName": realName,
            "P7_idCardType": "IDCARD",
            "P8_idCardNo": self._sm4_encrypt(idCardNo),  # sm4加密
            "P9_cardNo": self._sm4_encrypt(cardNo), # sm4加密
            "P10_year":"",
            "P11_month":"",
            "P12_cvv2":"",
            "P13_phone": self._sm4_encrypt(phone), # sm4加密
            "P14_currency": "CNY",
            "P15_orderAmount": amount, # 单位元
            "P16_goodsName": "测试订单",
            "P17_goodsDesc": "",
            "P18_terminalType":"IMEI",
            "P19_terminalId": "985211",  # 设备唯一识别码
            "P20_orderIp": "127.0.0.1",
            "P21_period":"",
            "P22_periodUnit":"",
            "P23_serverCallbackUrl": notify_url,

            # "sendValidateCode": "",
            "goodsQuantity": "1", # 商品数量
            "userAccount": "18202876063", # 用户账号
            # "enrollTime": "",
            # "lbs":"",
            "appType":"WX",
            "appName":"微信公众号",
            "dealSceneType": "REPAYMENT"
            # "dealSceneParams": {},
        }
        data = self._build_common_data('QuickPayFirstPayPreOrder', body)
        return self._request(url, headers=headers, data=data, verified="sign")
    

    def bind_card_preorder(self, user_id, realName, idCardNo, cardNo, phone, merchantNo=None):
        """鉴权绑卡预下单"""
        url = self.base_url + "/trx/quickPayApi/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'P2_customerNumber': merchantNo or self.merchant_number,
            "P3_userId": user_id,
            "P4_orderId": utils.parameter.generate_order_no("Bind"),
            "P5_timestamp": utils.parameter.generate_timestamp(),
            "P6_payerName": realName,
            "P7_idCardType": "IDCARD",
            "P8_idCardNo": self._sm4_encrypt(idCardNo),  # sm4加密
            "P9_cardNo": self._sm4_encrypt(cardNo), # sm4加密
            "P10_year":"",  # 信用卡有效期年份
            "P11_month":"",  # 信用卡有效期月份
            "P12_cvv2":"",
            "P13_phone": self._sm4_encrypt(phone), # sm4加密
            "sendValidateCode": "", # 是否同步下发短信
            "protocolType": "protocol" # 签约类型
        }
        data = self._build_common_data('QuickPayBindCardPreOrder', body)
        return self._request(url, headers=headers, data=data, verified="sign")
    

    def bind_card_SendValidateCode(self, order_no, merchantNo=None):
        """鉴权绑卡短信"""
        url = self.base_url + "/trx/quickPayApi/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'P2_customerNumber': merchantNo or self.merchant_number,
            "P3_orderId": order_no,
            "P4_timestamp": utils.parameter.generate_timestamp()
        }
        data = self._build_common_data('BindCardSendValidateCode', body)
        return self._request(url, headers=headers, data=data, verified="sign")
    

    def bind_card_Confirm(self, order_no, code, merchantNo=None):
        """鉴权绑卡"""
        url = self.base_url + "/trx/quickPayApi/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'P2_customerNumber': merchantNo or self.merchant_number,
            "P3_orderId": order_no,
            "P4_timestamp": utils.parameter.generate_timestamp(),
            "P5_validateCode": self._sm4_encrypt(code)
        }
        data = self._build_common_data('ConfirmBindCard', body)
        return self._request(url, headers=headers, data=data, verified="sign")


    def bind_card_unbind(self, user_id, bankbindId, merchantNo=None):
        """银行卡解绑"""
        url = self.base_url + "/trx/quickPayApi/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'P2_customerNumber': merchantNo or self.merchant_number,
            "P3_userId": user_id,
            "P4_bindId": bankbindId,
            "P5_orderId": utils.parameter.generate_order_no(),
            "P6_timestamp": utils.parameter.generate_timestamp()
        }
        data = self._build_common_data('BankCardUnbind', body)
        return self._request(url, headers=headers, data=data, verified="sign")
    